<?php

namespace app\servermessage\service\message;
use app\manage\service\ApiBase;

/**
 * @author:      李颖
 * @DateTime:    2017-06-08 13:43:49
 * @Description: 站内信
 */

class Message extends ApiBase
{
	
	protected $messageLogic = 'app\servermessage\logic\Message';
    protected $messageValidate = 'app\servermessage\validate\Message';
    protected $addFields = ['from_uid'=>0,'to_uid'=>[],'title'=>' ','content'=>' ','type'=>' ','orther_info'=>'{}'];

    /*一对多发送站内信*/
    protected function _sendBatchMessage(bool $isBatch): array 
    {

    	$data = $this->haveIssetReceiveData($this->addFields);
    	$scene = $isBatch ? 'sendBatch' : 'sendOne';

        $validate = validate($this->messageValidate);
        if(!$validate->scene($scene)->check($data)){  return verify_err(null, $validate->getError()); }
        if(!$validate->scene('checkOrtherInfo')->check($data['orther_info'])){ return verify_err(null, $validate->getError()); }

        return $this->logic($this->messageLogic)->sendBatchMessage($data);

    }

    /*一对一发送站内信*/
    public function sendOneMessage(): array 
    {
    	return $this->_sendBatchMessage(false);

    }

    /*一对多发送站内信*/
    public function sendBatchMessage(): array 
    {
    	return $this->_sendBatchMessage(true);
    }

    /*用户查看某站内信详情*/
    public function messageDetail(): array 
    {

    	$data = $this->receiveData(['id'=>0,'user_id'=>0]);

        $checkUserRes = $this->_checkBatchUserMsg($data, 'checkIdUid');
        if($checkUserRes['status'] != SUCCESS_STATUS){
            return $checkUserRes;
        }

        return $this->logic($this->messageLogic)->messageDetail($data);

    }
    
    //用户操作站内信时批量检验 接收到的数据、站内信、用户
    protected function _checkBatchUserMsg($data, $scene, $operate = ''): array 
    {

        $validate = validate($this->messageValidate);
        if(!$validate->scene($scene)->check($data)){
            return verify_err(null, $validate->getError());
        }

        is_numeric($data['id']) && $data['id'] = [$data['id']];
        $messages = $this->logic($this->messageLogic)->dbMessageList($data);
        if(!$messages){
            return verify_err(null,'要操作的站内信不存在');
        }
        if(count($messages) != count($data['id'])){
            return verify_err(null, '批量选择的站内信与查询到的站内信个数不一致');
        }
        foreach ($messages as $k => $message) {
            
            $errMsg = 'ID为' . $message['id'] . '的站内信';

            if( !in_array($data['user_id'], [$message['from_uid'],$message['to_uid']]) ){
                return verify_err($message, $errMsg . '不属于您');
            }
            if( $data['user_id'] == $message['from_uid'] && !in_array($message['is_del'], [0,2]) ){
                return verify_err($message, $errMsg . '已被删除');
            }
            if( $data['user_id'] == $message['to_uid'] && !in_array($message['is_del'], [0,1]) ){
                return verify_err($message, $errMsg . '已被删除');
            }
            if($operate == 'receive'){
                if($data['user_id'] != $message['to_uid']){
                    return verify_err($message, $errMsg . ',您不是接收方，不可操作');
                }
            }
            if($operate == 'send'){
                if($data['user_id'] != $message['from_uid']){
                    return verify_err($message, $errMsg . ',您不是发送方，不可操作');
                }
            }
        }

        return auto_success($message, '该用户可操作该站内信');

    }

    /*管理员查看某站内信详情*/
    public function adminMessageDetail(): array 
    {
        $data = $this->receiveData(['id'=>0]);
        return $this->stepRun($data, 'messageDetail', 'checkId');
    }

    /*查看某用户的收件箱、发件箱*/
    public function messageList(): array 
    {
        $data = $this->receiveData(['user_id'=>0,'msgType'=>' ','page'=>1,'listRows'=>15]);
        return $this->stepRun($data, 'messageList', 'userMsgList');
    }

     /*查看某用户的收件箱、发件箱*/
    public function adminMessageList(): array 
    {
        $data = $this->haveIssetReceiveData(['user_id'=>'','page'=>1,'listRows'=>15,'keywords'=>'']);
        $validate = validate($this->messageValidate);
        if(!$validate->scene('adminMessageList')->check($data)){
            return verify_err(null, $validate->getError());
        }
        return $this->logic($this->messageLogic)->adminMessageList($data); 
    }

    /*获取某用户未读站内信个数*/
    public function noReadCount(): array
    {
        $data = $this->receiveData(['user_id'=>0]);
        return $this->stepRun($data, 'noReadCount', 'checkUid');
    }

    /*将某站内信设为已读*/
    public function setOneRead(): array 
    {
        $data = $this->receiveData(['id'=>0,'user_id'=>0]);
        return $this->_setBatchRead($data, 'checkIdUid');
    }

    /*将多个站内信设为已读*/
    public function setBatchRead(): array 
    {
        $data = $this->receiveData(['id'=>[],'user_id'=>0]);
        return $this->_setBatchRead($data, 'checkIdsUid');
    }

    /*将站内信设为已读*/
    protected function _setBatchRead($data, $scene): array 
    {

        $checkUserRes = $this->_checkBatchUserMsg($data, $scene, 'receive');
        if($checkUserRes['status'] != SUCCESS_STATUS){
            return $checkUserRes;
        }

        return $this->logic($this->messageLogic)->setRead($data); 
    }

    /*用户回复某个站内信*/
    public function replyOneMessage(): array 
    {

        $data = $this->haveIssetReceiveData(['id'=>0,'user_id'=>0,'content'=>' ','orther_info'=>'{}']);

        $validate = validate($this->messageValidate);
        if(!$validate->scene('checkOrtherInfo')->check($data['orther_info'])){ return verify_err(null, $validate->getError()); }

        $checkUserRes = $this->_checkBatchUserMsg($data, 'reply', 'receive');
        if($checkUserRes['status'] != SUCCESS_STATUS){
            return $checkUserRes;
        }
prw($data);
        return $this->logic($this->messageLogic)->replyOneMessage($data);

    }

    /*用户删除某个站内信*/   
    public function delOneMessage(): array 
    {

        $data = $this->receiveData(['id'=>0,'user_id'=>0]);

        $checkUserRes = $this->_checkBatchUserMsg($data, 'checkIdUid');
        if($checkUserRes['status'] != SUCCESS_STATUS){
            return $checkUserRes;
        }

        return $this->logic($this->messageLogic)->delOneMessage($data);

    }

    /*用户批量删除站内信*/
    public function delBatchMessage(): array 
    {
        $data = $this->receiveData(['id'=>[],'user_id'=>0]);
       
        $checkUserRes = $this->_checkBatchUserMsg($data, 'checkIdsUid');
        if($checkUserRes['status'] != SUCCESS_STATUS){
            return $checkUserRes;
        }

        return $this->logic($this->messageLogic)->delBatchMessage($data);

    }

    /*管理员删除某个站内信*/
    public function adminDelOneMessage(): array 
    {
        $data = $this->receiveData(['id'=>0]);
        return $this->stepRun($data, 'adminDelMessage', 'checkId');

    }

    /*管理员批量删除某个站内信*/
    public function adminDelBatchMessage(): array 
    {
        $data = $this->receiveData(['id'=>[]]);
        return $this->stepRun($data, 'adminDelMessage', 'checkIds');
    }
}






